package Client.GUI;

import Client.logic.ClientIF;
import Server.DataBase.ReportIF;
import Server.DataBase.SchoolClass;
import Server.DataBase.Teacher;
import Server.Message.MessageReport;
import Server.Message.MessageReportReplay;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JComboBox;

import org.jfree.ui.RefineryUtilities;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class ReportsPanel extends MyJPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ReportIF report, id;
	public  ArrayList<Float> grade=null;
	public  ArrayList <String> thing=null;
	private JComboBox comboBoxReports, comboBoxInfo;
	private BarChart  bar;
	Teacher teacher;
	SchoolClass sClass;
	
	public ReportsPanel( ClientIF client) {
		
		super(PanelType.REPORTS_PANEL, client);
   
		BufferedImage myPic;
		try {
			myPic = ImageIO.read(new File("images"+ File.separator+"reports.jpg"));
		
			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
			lblAddClass.setBounds(29, 11, 699, 99);
			add(lblAddClass);
		
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		init();
		
	}
	
	public void init(){
		setLayout(null);
		initComboBox();
		getClient().sendMsgToServer(new MessageReport(1));
		MessageReport msg = (MessageReport) getClient().getMessageFromServer();
		
		JLabel lblReport = new JLabel("Report:");
		lblReport.setBounds(29, 137, 46, 14);
		add(lblReport);
		
		comboBoxReports.addItem("Choose");
		
		for (ReportIF rif : msg.getArray()) comboBoxReports.addItem(rif);
	}
	
	public void initComboBox(){
		comboBoxReports = new JComboBox();
		comboBoxReports.setBounds(125, 134, 206, 20);
		comboBoxReports.addActionListener(new ReportListener());
		add(comboBoxReports);
		
		comboBoxInfo = new JComboBox();
		comboBoxInfo.setBounds(125, 176, 206, 20);
		comboBoxInfo.addActionListener(new iDListener());
		add(comboBoxInfo);
	}
	
	private class ReportListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				report = (ReportIF) comboBoxReports.getSelectedItem();
			
				getClient().sendMsgToServer(new MessageReport(2, report.getId()));
				MessageReport msg = (MessageReport) getClient().getMessageFromServer();
			
				comboBoxInfo.removeAllItems();
				comboBoxInfo.addItem("Choose");
				for (ReportIF rif : msg.getArray()) 
					comboBoxInfo.addItem(rif);
				comboBoxInfo.setEnabled(true);
			} catch (java.lang.Throwable t){
			}
		}
	}
	
	private class iDListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				id = (ReportIF) comboBoxInfo.getSelectedItem();
			
				getClient().sendMsgToServer(new MessageReport(3, id.getId(), report.getId()));
				MessageReportReplay msg = (MessageReportReplay) getClient().getMessageFromServer();
			
				bar= new BarChart(id.toString(), msg.getValueArray(), msg.getStrArray());
				bar.pack();
		        RefineryUtilities.centerFrameOnScreen(bar);
		        bar.setVisible(true);	
			} catch (java.lang.Throwable t){
			}
		}
	}

	public MyJPanel pushPanel() {
		return new ReportsPanel(getClient());
	}
}
